package cn.jinjimi.app.store;

import android.database.sqlite.SQLiteDatabase;

import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.entity.ZdbProduct;
import cn.jinjimi.app.store.dao.ArticleDao;
import cn.jinjimi.app.store.dao.CacheDao;
import cn.jinjimi.app.store.dao.ClueEntityDao;
import cn.jinjimi.app.store.dao.ContactInfoDao;
import cn.jinjimi.app.store.dao.CustomerDao;
import cn.jinjimi.app.store.dao.LastFollowDao;
import cn.jinjimi.app.store.dao.NoticeEntityDao;
import cn.jinjimi.app.store.dao.ZdbProductAllDao;
import cn.jinjimi.app.store.dao.ZdbProductComDao;
import cn.jinjimi.app.store.entity.ArticleEntity;
import cn.jinjimi.app.store.entity.CacheData;
import cn.jinjimi.app.store.entity.ClueEntity;
import cn.jinjimi.app.store.entity.NoticeEntity;
import cn.jinjimi.app.store.entity.custom.ContactInfo;
import cn.jinjimi.app.store.entity.custom.LastFollow;

import java.util.Map;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.identityscope.IdentityScopeType;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by Jinsen on 16/2/22.
 */
public class DaoSession extends AbstractDaoSession {

    private DaoConfig mArticleDaoConfig;
    private DaoConfig mCacheDaoConfig;
    private DaoConfig mCustomerDaoConfig;
    private DaoConfig mZdbProductAllDaoConfig;
    private DaoConfig mZdbProductComDaoConfig;
    private final DaoConfig noticeEntityDaoConfig;
    private final DaoConfig clueEntityDaoConfig;
    private final DaoConfig lastFollowDaoConfig;
    private final DaoConfig contactInfoDaoConfig;

    private ArticleDao mArticleDao;
    private CacheDao mCacheDao;
    private CustomerDao mCustomerDao;
    private ZdbProductAllDao mZdbProductAllDao;
    private ZdbProductComDao mZdbProductComDao;
    private final NoticeEntityDao noticeEntityDao;
    private final ClueEntityDao clueEntityDao;
    private final LastFollowDao lastFollowDao;
    private final ContactInfoDao contactInfoDao;

    public DaoSession(SQLiteDatabase db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig> daoConfigMap) {
        super(db);

        this.mArticleDaoConfig = daoConfigMap.get(ArticleDao.class).clone();
        mArticleDaoConfig.initIdentityScope(type);

        mCacheDaoConfig = daoConfigMap.get(CacheDao.class).clone();
        mCacheDaoConfig.initIdentityScope(type);

        mCustomerDaoConfig = daoConfigMap.get(CustomerDao.class).clone();
        mCustomerDaoConfig.initIdentityScope(type);

        mZdbProductAllDaoConfig = daoConfigMap.get(ZdbProductAllDao.class).clone();
        mZdbProductAllDaoConfig.initIdentityScope(type);

        mZdbProductComDaoConfig = daoConfigMap.get(ZdbProductComDao.class).clone();
        mZdbProductComDaoConfig.initIdentityScope(type);

        noticeEntityDaoConfig = daoConfigMap.get(NoticeEntityDao.class).clone();
        noticeEntityDaoConfig.initIdentityScope(type);

        clueEntityDaoConfig = daoConfigMap.get(ClueEntityDao.class).clone();
        clueEntityDaoConfig.initIdentityScope(type);

        lastFollowDaoConfig = daoConfigMap.get(LastFollowDao.class).clone();
        lastFollowDaoConfig.initIdentityScope(type);

        contactInfoDaoConfig = daoConfigMap.get(ContactInfoDao.class).clone();
        contactInfoDaoConfig.initIdentityScope(type);


        mArticleDao = new ArticleDao(this.mArticleDaoConfig, this);
        mCacheDao = new CacheDao(this.mCacheDaoConfig, this);
        mCustomerDao = new CustomerDao(this.mCustomerDaoConfig, this);
        mZdbProductAllDao = new ZdbProductAllDao(this.mZdbProductAllDaoConfig, this);
        mZdbProductComDao = new ZdbProductComDao(this.mZdbProductComDaoConfig, this);
        noticeEntityDao = new NoticeEntityDao(noticeEntityDaoConfig, this);
        clueEntityDao = new ClueEntityDao(clueEntityDaoConfig, this);
        lastFollowDao = new LastFollowDao(lastFollowDaoConfig, this);
        contactInfoDao = new ContactInfoDao(contactInfoDaoConfig, this);

        this.registerDao(ArticleEntity.class, mArticleDao);
        this.registerDao(CacheData.class, mCacheDao);
        this.registerDao(Custom.class, mCustomerDao);
        this.registerDao(ZdbProduct.class, mZdbProductAllDao);
        this.registerDao(ZdbProduct.class, mZdbProductComDao);
        this.registerDao(NoticeEntity.class, noticeEntityDao);
        this.registerDao(ClueEntity.class, clueEntityDao);
        this.registerDao(LastFollow.class, lastFollowDao);
        this.registerDao(ContactInfo.class, contactInfoDao);
    }

    public void clear() {
        this.mArticleDaoConfig.getIdentityScope().clear();
        this.mCacheDaoConfig.getIdentityScope().clear();
        this.mZdbProductAllDaoConfig.getIdentityScope().clear();
        this.mZdbProductComDaoConfig.getIdentityScope().clear();
        this.noticeEntityDaoConfig.getIdentityScope().clear();
        this.clueEntityDaoConfig.getIdentityScope().clear();
        this.lastFollowDaoConfig.getIdentityScope().clear();
        this.contactInfoDaoConfig.getIdentityScope().clear();
    }

    public ArticleDao getArticleDao() {
        return this.mArticleDao;
    }

    public CacheDao getCacheDao() {
        return this.mCacheDao;
    }

    public CustomerDao getCustomerDao() {
        return this.mCustomerDao;
    }

    public ZdbProductAllDao getZdbProductAllDao() {
        return mZdbProductAllDao;
    }

    public ZdbProductComDao getZdbProductComDao() {
        return mZdbProductComDao;
    }

    public NoticeEntityDao getNoticeEntityDao() {
        return noticeEntityDao;
    }

    public ClueEntityDao getClueEntityDao() {
        return clueEntityDao;
    }

    public LastFollowDao getLastFollowDao() {
        return lastFollowDao;
    }

    public ContactInfoDao getContactInfoDao() {
        return contactInfoDao;
    }

}